<script setup>
  import { Suspense } from 'vue'
  import { collectOptions } from '@/helpers/context.js'
  import TaskAddFormComponent from './components/TaskAddFormComponent.vue'
  import { useTaskConfigStore } from '@/stores/task'

  const options = collectOptions()

  const redirectOnsave = (task) => {
    const typeLabel = task.type_.replace('internal', '')
    window.location.replace('/' + typeLabel + 's/' + task.id)
  }
  const redirectOnCancel = () => {
    if (history.length > 1) {
      history.back()
    } else {
      window.location = '/'
    }
  }
</script>

<template>
  <Suspense>
    <div class="limited_width width30">
      <TaskAddFormComponent
        :initialData="options.initial_data"
        :url="options.api_url"
        :formConfigUrl="options.form_config_url"
        @saved="redirectOnsave"
        @cancel="redirectOnCancel"
      ></TaskAddFormComponent>
    </div>
    <template #fallback> Loading... </template>
  </Suspense>
</template>
